<?php
//installs a new language in the system
function language_install($lng = NULL) {
	global $conf;

	if(!$lng)
	$lng = $_GET['language'];

	if(!perms_check('admin', 'modules')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db = new dbquery;
	$db2 = new dbquery;

	$db->query("SELECT * FROM $conf[prefix]lang_fields") or $db->err(__FILE__, __LINE__);

	while($d = $db->fetch_object()) {
		$fields = explode(';', $d->fields);
		$types = explode(';', $d->types);
		$defaults = explode(';', $d->defaults);

		$query = "ALTER TABLE $conf[prefix]$d->table";
		for($i = 0; $i < count($fields); $i++) {
			if($i != 0)
			$query .= ",";
			$query .= " ADD ".string_template($fields[$i], array('lang' => $lng))." $types[$i]";
			if($defaults[$i] != 'NULL')
			$query .= " DEFAULT '$defaults[$i]' NOT NULL";
		}
		$db2->query($query) or $db2->err(__FILE__, __LINE__);
	}

	$db->query("ALTER TABLE $conf[prefix]lang ADD $lng TEXT NULL") or $db->err(__FILE__, __LINE__);
	
	//clear sql_cache
	$db->clear_cache('langnames.cache', 'lang');
	$db->clear_cache('langs.cache', 'lang');
	
	//updating LANG table
	if(@include("$lng.lang")) {
		foreach($language_upd as $field => $value) {
			$db->query("UPDATE $conf[prefix]lang SET $lng = '".str_replace("\\\\", "\\", $value)."' WHERE name = '$field'") or $db->err(__FILE__, __LINE__);
		}
	}

	redirect($_SESSION['redirect_1']);
	exit;
}

//updates a language in the system
function language_update($lng = NULL) {
	global $conf;

	if(!$lng)
	$lng = $_GET['lang'];

	if(!perms_check('admin', 'modules')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$av_langs = get_langs_list();

	if(!in_array($lng, $av_langs)) {
		redirect($_SESSION['redirect_1']);
		exit;
	}

	//updating LANG table
	if(@include("$lng.lang")) {
		foreach($language_upd as $field => $value) {
			$db->query("UPDATE $conf[prefix]lang SET $lng = '".str_replace("\\\\", "\\", $value)."' WHERE name = '$field'") or $db->err(__FILE__, __LINE__);
		}
	}

	//clear sql_cache
	$db=new dbquery;
	$db->clear_cache('langnames.cache', 'lang');
	$db->clear_cache('langs.cache', 'lang');	
	
	redirect($_SESSION['redirect_1']);
	exit;
}

//uninstalls a language in the system
function language_uninstall($lng = NULL) {
	global $conf;

	if(!perms_check('admin', 'modules')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	if(!$lng)
	$lng = $_GET['language'];

	if($conf['language'] == $lng) {
		redirect($_SESSION['redirect_2']);
		exit;
	}

	$db = new dbquery;
	$db2 = new dbquery;

	$db->query("UPDATE $conf[prefix]users SET lang = '$conf[language]' WHERE lang = '$lng'") or $db->err(__FILE__, __LINE__);

	if($_SESSION['lang_short'] == $lng) {
		$_SESSION['lang_short'] = $conf['language'];
	}

	$db->query("SELECT * FROM $conf[prefix]lang_fields") or $db->err(__FILE__, __LINE__);

	while($d = $db->fetch_object()) {
		$fields = explode(';', $d->fields);

		$query = "ALTER TABLE $conf[prefix]$d->table";
		for($i = 0; $i < count($fields); $i++) {
			if($i != 0)
			$query .= ",";
			$query .= " DROP ".string_template($fields[$i], array('lang' => $lng));
		}
		$db2->query($query) or $db2->err(__FILE__, __LINE__);
	}
	
	//clear sql_cache
	$db->clear_cache('langnames.cache', 'lang');
	$db->clear_cache('langs.cache', 'lang');
	
	$db2->query("ALTER TABLE $conf[prefix]lang DROP $lng") or $db2->err(__FILE__, __LINE__);
	redirect($_SESSION['redirect_2']);
	exit;
}

//edycja bazy jezyk�w
function lang_edit() {
	global $conf, $_GET;

	if(!perms_check('admin', 'config')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db = new dbquery;

	foreach($_POST as $klucz=>$l){
		$v=str_replace("[php]","<?", $l);
		$v=str_replace("[/php]","?>", $v);
		$v=str_replace("&amp;lt;", "<", $v);
		$v=str_replace("&amp;gt;", ">", $v);
		$key=end($foo=explode("_", $klucz));
		$name=str_replace("_".$key, "", $klucz);
		
		$v=post_text($v);
      
		$db->query("UPDATE $conf[prefix]lang SET $key='$v' WHERE name='$name'");
	}

	$db->clear_cache('lang_*', 'lang');

	redirect($_SESSION['redirect_1']);
	exit;
}
?>
